options(width = 255);


main <- function(dir) {
    #z <- lapply(c("hours", "bytes", "edits"), function(m) { analyze(dir, m); });
    z <- lapply(c("bytes", "edits"), function(m) { analyze(dir, m); });
}

analyze <- function(dir, metric) {
    z <- print(sprintf("=================Analyzing metric %s=================", metric));
    path <- sprintf("%s/%s.txt", dir, metric);
    data <- read.table(file=path, sep="\t", quote="", comment.char="", header=TRUE);
    z <- lapply(c("Article", "Article.Talk", "User", "User.Talk", "Other", "Maintenance"),
                function(ns) { analyze_ns(data, ns) });
    print("");
}

analyze_ns <- function(data, ns) {
    print(sprintf("Analyzing namespace %s:", ns));
    fit <- glm(data[,ns] ~ factor(data$Cohort) + factor(data$Year) + factor(data$Offset) + factor(data$Lifespan) + factor(data$Current), family="binomial");
    print(drop1(fit, test="Chisq"))
    print(summary(fit));
}

args <- commandArgs(TRUE);
z <- main(args[1]);
